package com.ruoyi.common.enums.wecom;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Getter
@AllArgsConstructor
public enum WecomChatGroupStatusEnum {

    NORMAL(0, "跟进人正常"),
    OWNER_DIMISSION(1, "跟进人离职"),
    DIMISSION_INHERITING(2, "离职继承中"),
    DIMISSION_INHERIT_COMPLETED(3, "离职继承完成");

    private final Integer code;
    private final String desc;

    private static final Map<Integer, WecomChatGroupStatusEnum> ENUM_MAP;

    static {
        WecomChatGroupStatusEnum[] values = values();
        Map<Integer, WecomChatGroupStatusEnum> map = new HashMap<>(values.length);
        for (WecomChatGroupStatusEnum value : values) {
            map.put(value.getCode(), value);
        }
        ENUM_MAP = Collections.unmodifiableMap(map);
    }

    /**
     * 根据状态码查找枚举
     */
    public static WecomChatGroupStatusEnum getByCode(Integer code) {
        WecomChatGroupStatusEnum enumValue = ENUM_MAP.get(code);
        if (ObjectUtil.isEmpty(enumValue)) {
            throw new RuntimeException("客户群跟进状态码有误: " + code);
        }
        return enumValue;
    }

    /**
     * 检查状态码是否有效
     */
    public static boolean isValid(Integer code) {
        return ENUM_MAP.containsKey(code);
    }

    /**
     * 判断是否为正常状态
     */
    public static boolean isNormal(Integer code) {
        return NORMAL.getCode().equals(code);
    }

    /**
     * 判断是否为离职相关状态
     */
    public static boolean isDimissionRelated(Integer code) {
        return OWNER_DIMISSION.getCode().equals(code) ||
            DIMISSION_INHERITING.getCode().equals(code) ||
            DIMISSION_INHERIT_COMPLETED.getCode().equals(code);
    }

    /**
     * 判断是否正在继承中
     */
    public static boolean isInheriting(Integer code) {
        return DIMISSION_INHERITING.getCode().equals(code);
    }

    /**
     * 判断继承是否完成
     */
    public static boolean isInheritCompleted(Integer code) {
        return DIMISSION_INHERIT_COMPLETED.getCode().equals(code);
    }
}
